package com.fromfuture.xuhuidoc.android.fragments;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.BroadcastReceiver.MyConsultRoomBroadcastReceiver;
import com.fromfuture.xuhuidoc.android.activity.MyConsultRoomActivity;
import com.fromfuture.xuhuidoc.android.adapter.MyConsultRoomAdapter;
import com.fromfuture.xuhuidoc.android.bean.MyConsultRoomRecycleViewInfo;
import com.fromfuture.xuhuidoc.android.callback.MyConsultRoomFra01ToFra02;
import com.fromfuture.xuhuidoc.android.callback.MyConsultRoomFra02ToFra01;
import com.fromfuture.xuhuidoc.android.callback.MyConsultRoomFragment02Fresh;
import com.fromfuture.xuhuidoc.android.callback.MyConsultRoomFragment02FreshRecycleview;
import com.fromfuture.xuhuidoc.android.callback.OtherPatExitCallBack;
import com.fromfuture.xuhuidoc.android.customview.DividerItemDecoration;
import com.fromfuture.xuhuidoc.android.customview.MyItemAnimator;
import com.fromfuture.xuhuidoc.cordova.plugin.MyConsultRoomPlugin;
import com.fromfuture.xuhuidoc.cordova.plugin.MySystemPlugin;

import org.apache.cordova.CallbackContext;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author：何送军
 * creation time：2017/8/26
 **/
public class MyConsultRoomFragment01 extends Fragment implements MyConsultRoomFra02ToFra01{
    CallbackContext callbackContext;
    private View rootview;

    private RecyclerView recyclerView;
    private List<MyConsultRoomRecycleViewInfo>list;
    private List<String>listItemInfo;
    private List<Integer> list_isButtonState;
    private MyConsultRoomRecycleViewInfo myConsultRoomRecycleViewInfo;
    MyConsultRoomAdapter myConsultRoomAdapter;
    MySystemPlugin mySystemPlugin;
    MyConsultRoomPlugin myConsultRoomPlugin;
    int num=0;

    MyConsultRoomBroadcastReceiver myConsultRoomBroadcastReceiver;

    //接口
    MyConsultRoomFra01ToFra02 myConsultRoomFra01ToFra02;
    MyConsultRoomFragment02FreshRecycleview myConsultRoomFragment02FreshTellActivity;

//    public void setMyConsultRoomFragment02FreshTellActivity(MyConsultRoomFragment02FreshTellActivity myConsultRoomFragment02FreshTellActivity) {
//        this.myConsultRoomFragment02FreshTellActivity = myConsultRoomFragment02FreshTellActivity;
//    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.activity_my_consult_room_01,container,false);
        ConsultRoom_init();
        return rootview;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    /**
     * 初始化
     * */
    protected void ConsultRoom_init(){
        mySystemPlugin=new MySystemPlugin();
        myConsultRoomPlugin=new MyConsultRoomPlugin();
        myConsultRoomFra01ToFra02= (MyConsultRoomFra01ToFra02) getFragmentManager().findFragmentByTag("myConsultRoomFragment02");
        myConsultRoomFragment02FreshTellActivity= (MyConsultRoomFragment02FreshRecycleview) getFragmentManager().findFragmentByTag("myConsultRoomFragment02");
        listItemInfo=new ArrayList<String>();
        ConsultRoom_findViewById();
        onClick();
        ConsultRoom_recyclerView();
        callback();
        additem();

    }

    /**
     * 查找findViewById
     * */
    protected void ConsultRoom_findViewById(){
        recyclerView= (RecyclerView) rootview.findViewById(R.id.my_consult_room_01_recyclerView);

    }

    /**
     * 点击事件
     * */
    private void onClick(){

    }

    protected void ConsultRoom_recyclerView(){
        list=new ArrayList<MyConsultRoomRecycleViewInfo>();
        list_isButtonState=new ArrayList<Integer>();

        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
        recyclerView.setItemAnimator(new MyItemAnimator());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()
                , LinearLayoutManager.VERTICAL, false
        );
        recyclerView.setLayoutManager(linearLayoutManager);

        myConsultRoomAdapter=new MyConsultRoomAdapter(list,list_isButtonState,listItemInfo,getActivity());
        recyclerView.setAdapter(myConsultRoomAdapter);

        myConsultRoomAdapter.setButton_state_listener(new MyConsultRoomAdapter.MyConsultRoom_button_state_listener() {
            @Override
            public void button_state(int pos,int state) {
                myConsultRoomAdapter.changeButtonState(pos,state);
            }
        });

        myConsultRoomAdapter.setOnItemClickListener(new MyConsultRoomAdapter.MyConsultRoom_onItemClickListener() {
            @Override
            public void onItemClickListener(int pos,int state) {
                MyConsultRoomActivity.onItemClickDoing(pos,state);
            }
        });
        myConsultRoomAdapter.setOnItemLongClickListener(new MyConsultRoomAdapter.MyConsultRoom_onItemLongClickListener() {
            @Override
            public void onItemLongClickListener(int pos,int state) {
                MyConsultRoomActivity.onItemLongClickDoing(pos,state);
            }
        });
        myConsultRoomAdapter.setToMyConsultRoom_02_Listener(new MyConsultRoomAdapter.ToMyConsultRoom_02_Listener() {
            @Override
            public void toMyConsult() {
                MyConsultRoomActivity.toMyConsultRoom02();
            }
        });
        myConsultRoomAdapter.setMyConsultRoomFragment02Fresh(new MyConsultRoomFragment02Fresh() {
            @Override
            public void fragment02Fresh() {
                myConsultRoomFragment02FreshTellActivity.refresh();
            }
        });
    }


    /**
     * 有新的病人进入诊室，recycleview自动添加新的病人item，并把新增的病人信息发给fra02
     */
    private void additem() {
        myConsultRoomBroadcastReceiver = new MyConsultRoomBroadcastReceiver(
                new MyConsultRoomBroadcastReceiver.Haspatin_interface() {
                    @Override
                    public void haspatin(String info) {
                        try {
                            JSONObject myJsonObject = new JSONObject(info);
                            myConsultRoomRecycleViewInfo = new MyConsultRoomRecycleViewInfo();
                            myConsultRoomRecycleViewInfo.setConsultname("名字: " + myJsonObject.getString("Pic_name"));
                            myConsultRoomRecycleViewInfo.setConsultpic(myJsonObject.getString("Pic_url"));
                            myConsultRoomRecycleViewInfo.setConsultcontents("内容");
                            myConsultRoomRecycleViewInfo.setConsultPatGuid(myJsonObject.getString("Pat_guid"));
                            myConsultRoomAdapter.setjiezhenPatGuid(myJsonObject.getString("Pat_guid"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                        String t = format.format(new Date());
                        myConsultRoomRecycleViewInfo.setConsulttime(t);
                        if (!list.contains(myConsultRoomRecycleViewInfo)) {
                            list.add(myConsultRoomRecycleViewInfo);
                        }
                        list_isButtonState.add(0);
                        setNum(getNum() + 1);
                        myConsultRoomAdapter.notifyDataSetChanged();
                        listItemInfo.add(info);

                    }});

    }

    //接口回调的方法
    private void callback(){

        myConsultRoomPlugin.setOtherPatExitCallBack(new OtherPatExitCallBack() {
            @Override
            public void other_pat_exit(String patguid,String patname) {
                if (listItemInfo.size()>0){
                    for (int i=0;i<listItemInfo.size();i++){
                        String jsonObject=list.get(i).getConsultPatGuid();
                        if (jsonObject.equals(patguid)){
                            Toast.makeText(getActivity(),"未接诊患者："+patname+" 已经退出您的诊室",Toast.LENGTH_LONG).show();
                            myConsultRoomAdapter.changeButtonState01(i,3);
                        }
                    }
                }
            }
        });

    }


    //来自碎片2,患者退出诊室
    @Override
    public void setArgs(Bundle args) {
        String pos=args.getString("exit_pos");
        Log.d("我是我是hsj是是是是对方的身份",args.getString("exit_pos"));
//        listItemInfo.get()
        myConsultRoomAdapter.changeButtonState(Integer.parseInt(pos),2);
    }

}
